﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace DBA.Data.Hibernate
{
    /// <summary>
    /// NHibernate专用查询条件类
    /// </summary>
    public class Condition
    {
        #region Condition的构造器
        /// <summary>
        /// 普通查询构造器
        /// </summary>
        /// <param name="name">字段名</param>
        /// <param name="con">查询条件</param>
        /// <param name="obj">查询的值</param>
        public Condition(string name, Op con, object obj)
        {
            if (con != Op.Between && con != Op.In && con != Op.OrderBy)
            {
                _name = name;
                _opType = con;
                _value1 = obj;
            }
            else
            {
                throw new Exception("错误！！使用Between和In以及OrderBy查询时，请使用Condition类的相应构造器");
            }
        }

        /// <summary>
        /// Between查询构造器
        /// </summary>
        /// <param name="name">Between字段名</param>
        /// <param name="obj1">查询开始值</param>
        /// <param name="obj2">查询结束值</param>
        public Condition(string name, object objStart, object objEnd)
        {
            _name = name;
            _opType = Op.Between;
            _value1 = objStart;
            _value2 = objEnd;
        }

        /// <summary>
        /// Or查询构造器
        /// </summary>
        /// <param name="name">Or字段名</param>
        /// <param name="name">Or查询的类型</param>
        /// <param name="value1">查询值1</param>
        /// <param name="value2">查询值2</param>
        public Condition(string name,OrQuery orType, object value1, object value2)
        {
           
                _name = name;
                _opType = Op.Or;
                _value1 = value1;
                _value2 = value2;
                _orType = orType;
        }
        /// <summary>
        /// In查询构造器
        /// </summary>
        /// <param name="name">In字段名</param>
        /// <param name="objs">查询值数组</param>
        public Condition(string name, object[] objs)
        {
            _name = name;
            _opType = Op.In;
            _values = objs;
        }

        /// <summary>
        /// NotIn查询构造器
        /// </summary>
        /// <param name="name">In字段名</param>
        /// <param name="objs">查询值数组</param>
        public Condition(string name, Op con, object[] objs)
        {
            _name = name;
            _opType = con;
            _values = objs;
        }
        /// <summary>
        /// 排序查询构造器
        /// </summary>
        /// <param name="name">OrderBy的字段名</param>
        /// <param name="order">是否倒序</param>
        public Condition(string name, bool order)
        {
            _name = name;
            _opType = Op.OrderBy;
            _value1 = order;
        }
        /// <summary>
        /// 表链接查询构造器
        /// </summary>
        /// <param name="tablename">主表中子表的集合名称</param>
        public Condition(string tablename)
        {
            _opType = Op.TableLink;
            _value1 = tablename;
        }
        #endregion

        #region 属性
        private string _name;
        /// <summary>
        /// 要查询的字段名
        /// </summary>
        public string Name
        {
            get { return _name; }
        }
        private Op _opType;
        /// <summary>
        /// 查询条件枚举(如果是Between查询请设置Value2的值，In查询请使用Values)
        /// </summary>
        public Op OpType
        {
            get { return _opType; }
        }
        private object _value1;
        /// <summary>
        /// 字段名的值
        /// </summary>
        public object Value1
        {
            get { return _value1; }
        }
        private object _value2;
        /// <summary>
        /// Between查询请设置Value2的值
        /// </summary>
        public object Value2
        {
            get { return _value2; }
        }
        private object[] _values;
        /// <summary>
        /// In查询请使用Values,传入一个数组
        /// </summary>
        public object[] Values
        {
            get { return _values; }
        }
        private OrQuery _orType;
        /// <summary>
        /// Or查询类型
        /// </summary>
        public OrQuery OrType
        {
            get { return _orType; }
        }
        #endregion
    }
    /// <summary>
    /// Or查询类型枚举
    /// </summary>
    public enum OrQuery
    {
        /// <summary>
        /// 表示 =|=
        /// </summary>
        EqAndEq = 0
    }
    #region 表示条件的枚举
    /// <summary>
    /// 表示条件的枚举
    /// </summary>
    public enum Op
    {
        /// <summary>
        /// 表示"大于等于"
        /// </summary>
        Ge = 0,
        /// <summary>
        /// 表示"大于"
        /// </summary>
        Gt = 1,
        /// <summary>
        /// 表示"小于等于"
        /// </summary>
        Le = 2,
        /// <summary>
        /// 表示"小于"
        /// </summary>
        Lt = 3,
        /// <summary>
        /// 表示"Between"查询(请使用Condition类的Between查询构造器)
        /// </summary>
        Between = 4,
        /// <summary>
        /// 表示"等于"
        /// </summary>
        Eq = 5,
        /// <summary>
        /// 模糊查询
        /// </summary>
        Like = 6,
        /// <summary>
        /// 表示in查询(请使用Condition类的In查询构造器)
        /// </summary>
        In = 7,
        /// <summary>
        /// 表示排序（请使用Condition类的排序查询构造器）
        /// </summary>
        OrderBy = 8,
        /// <summary>
        /// 表示“不等于”
        /// </summary>
        NotEq = 9,
        /// <summary>
        /// 表示“不在其中”
        /// </summary>
        NotIn = 10,
        /// <summary>
        /// 表示“不在之间”
        /// </summary>
        NotBetween = 11,
        /// <summary>
        /// 链接子表
        /// </summary>
        TableLink = 12,
        /// <summary>
        /// 表示“Or”查询
        /// </summary>
        Or = 13
    }
    #endregion
}
